package com.jp.refreshlocalcacheserver.net.netty.message;

import com.jp.refreshlocalcacheserver.net.netty.thread.SendMessagePoolService;
import com.jp.refreshlocalcacheserver.utils.ApplicationContextUtil;
import io.netty.channel.ChannelHandlerContext;

import static com.jp.refreshlocalcacheserver.net.netty.EchoServerHandlerInitializer.delimiter;

/**
 * @Author: jipeng
 * @Description: 发送消息
 * @Date: Created in 2020/5/18 20:53
 */
public class ResponseMessage implements Runnable {

    protected Integer msgId;
    protected String context;
    protected ChannelHandlerContext ctx;


    public Integer getMsgId() {
        return msgId;
    }

    public void setMsgId(Integer msgId) {
        this.msgId = msgId;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    /**
     * 对指定的通道发送消息
     * @param ctx
     */
    public void sendTo(ChannelHandlerContext ctx){
        this.ctx = ctx;
        SendMessagePoolService sendMessagePoolService =  ApplicationContextUtil.getBean(SendMessagePoolService.class);
        sendMessagePoolService.execut(this);
    }

    @Override
    public void run() {
        StringBuilder sb = new StringBuilder().append(msgId).append(",").append(context).append(delimiter);
        ctx.writeAndFlush(sb.toString());
    }
}
